package org.mili.ticketeer.service;

import org.mili.application.service.Service;
import org.mili.application.service.ServiceException;
import org.mili.ticketeer.dao.ContactTicketDao;
import org.mili.application.dao.DaoFactory;
import org.mili.application.dao.ModificationResult;
import org.mili.ticketeer.dao.TicketDao;
import org.mili.ticketeer.dto.Contact;
import org.mili.ticketeer.dto.Ticket;
import org.mili.application.util.Lambda;

import java.util.List;

public class TicketService extends Service {
	private TicketDao _ticketDao = DaoFactory.getDao(TicketDao.class);
    private ContactTicketDao _contactTicketDao = DaoFactory.getDao(ContactTicketDao.class);

    public ModificationResult delete(final Ticket ticket) throws ServiceException {
        return doInService(new Lambda<ModificationResult>() {
            @Override
            public ModificationResult exec(Object... params) throws Exception {
                return _ticketDao.delete(ticket);
            }
        });
    }

    public ModificationResult write(final Ticket ticket) throws ServiceException {
        return doInService(new Lambda<ModificationResult>() {
            @Override
            public ModificationResult exec(Object... params) throws Exception {
                return _ticketDao.write(ticket);
            }
        });
    }

    public Boolean assign(final Contact contact, final Ticket ticket) throws ServiceException {
        return doInService(new Lambda<Boolean>() {
            @Override
            public Boolean exec(Object... params) throws Exception {
                return _contactTicketDao.assign(contact, ticket);
            }
        });
    }

    public List<Ticket> getTickets() throws ServiceException {
        return doReadInService(new Lambda<List<Ticket>>() {
            @Override
            public List<Ticket> exec(Object... params) throws Exception {
                return _ticketDao.getTickets();
            }
        });
    }

    public List<Contact> getAssignmentsFor(final Ticket ticket) throws ServiceException {
        return doReadInService(new Lambda<List<Contact>>() {
            @Override
            public List<Contact> exec(Object... params) throws Exception {
                return _contactTicketDao.getAssignmentsFor(ticket);
            }
        });
    }


}
